﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Transactions;
using System.Xml.Linq;

public partial class Photo : System.Web.UI.Page
{
    protected string fileName = string.Empty;

    protected void Page_Load(object sender, EventArgs e)
    {
        string photoId = this.Request.QueryString["photoId"];
        if (photoId == null)
        {

        }

        using (
            SqlConnection conn = new SqlConnection(
            ConfigurationManager.ConnectionStrings["PhotoDb"].ConnectionString))
        {
            conn.Open();
            string url = SqlHelper.GetUrl(photoId, conn);
            this.ImagePhoto.Src = url;
            this.ImagePhoto.Visible = true;
        }
    }

    protected void ButtonModify_Click(object sender, EventArgs e)
    {
        string photoId = this.Request.QueryString["photoId"];
        this.Response.Redirect(string.Format("SetPhoto.aspx?PhotoId={0}", photoId));
    }

    protected void ButtonDelete_Click(object sender, EventArgs e)
    {
        try
        {
            using (TransactionScope scope = new TransactionScope())
            {
                using (SqlConnection conn = new SqlConnection(
                    ConfigurationManager.ConnectionStrings["PhotoDb"].ConnectionString))
                {
                    conn.Open();
                    string photoId = this.Request.QueryString["photoId"];
                    string url = SqlHelper.GetUrl(photoId, conn);

                    SimpleFS.Services.Transactions.Implementations.SimpleResourceManager resourceManager
                        = new SimpleFS.Services.Transactions.Implementations.SimpleResourceManager();

                    SimpleFS.Storage.Windows.File file
                        = new SimpleFS.Storage.Windows.File(url);

                    SimpleFS.Services.Transactions.File txFile
                        = new SimpleFS.Services.Transactions.File(file, resourceManager);

                    SqlCommand command = new SqlCommand("delete photos where photoId = @PhotoId", conn);

                    SqlParameter paramId = new SqlParameter("@PhotoId", photoId);
                    command.Parameters.Add(paramId);

                    command.Connection = conn;
                    command.ExecuteNonQuery();

                    txFile.Delete();
                }
                scope.Complete();
            }
        }
        catch(Exception ex)
        {
            this.Response.Redirect(
                string.Format("Error.aspx?Message={0}", ex.Message));
        }
        
        this.Response.Redirect("Default.aspx");
    }

    protected void ButtonCancel_Click(object sender, EventArgs e)
    {
        this.Response.Redirect("Default.aspx");
    }
}
